package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.MemberRank;
import com.jfinal.kit.Ret;

import java.util.List;

/**
 * @Author KingMo
 * @Date 2016/12/19 16:56
 * @Description 会员等级后台控制器
 */
public class MemberRankController extends BaseAdminController<MemberRank> {
	
	private MemberRank memberRank;
	private String shopId;
	// 列表
	public void list() {
		render("member_rank_list.html");
	}

	public void ajaxlist(){
		shopId=getAttr("shopId");
		List<MemberRank> lpa = MemberRank.dao.find("select * from mall_member_rank where shopId = ? order by createDate desc",shopId);
		renderJson(lpa);
	}
	
	// 添加
	public void add() {
		render("member_rank_input.html");
	}

	// 编辑
	public void edit() {
		id = getPara("id","");
		memberRank = MemberRank.dao.findById(id);
		setAttr("memberRank", memberRank);
		render("member_rank_input.html");
	}
		
	// 是否已存在 ajax验证	
	public void checkName(){
		String newValue = getPara("memberRank.name","");
		if (isUnique("memberRank","name",newValue)) {
			renderText("true");
		} else {
			renderText("false");
		}
	}
	
	// 添加
	//@Before(MemberRankValidator.class)
	public void save(){
		shopId=getAttr("shopId");
		memberRank = getModel(MemberRank.class);
		String regNum="^\\d+$";
		if(!memberRank.getPreferentialScale().toString().matches(regNum) || memberRank.getPreferentialScale()>100){
			renderJson(Ret.fail("info","优惠百分比请输入0-100的正整数!"));
			return;
		}
		if(!memberRank.getPoint().toString().matches(regNum)){
			renderJson(Ret.fail("info","所需积分请填写0或正整数字!"));
			return;
		}
		if(!memberRank.getMoneyNum().toString().matches(regNum)){
			renderJson(Ret.fail("info","所需消费金额请填写0或正整数字!"));
			return;
		}

		if (MemberRank.dao.getMemberRankByPoint(memberRank.getInt("point"),shopId) != null) {
			renderJson(Ret.fail("info","已存在相同积分的会员等级!"));
			return;
		}
		memberRank.set("shopId", shopId);
		memberRank.save(memberRank);
		renderJson(Ret.ok("info","添加成功!").set("url","list"));
	}
	
	// 更新
	//@Before(MemberRankValidator.class)
	public void update(){
		shopId=getAttr("shopId");
		memberRank = getModel(MemberRank.class);
		String regNum="^\\d+$";
		MemberRank persistent = MemberRank.dao.findById(memberRank.getStr("id"));//修改之前的等级信息
		MemberRank equalPointMemberRank = MemberRank.dao.getMemberRankByPoint(memberRank.getInt("point"),shopId);
		if(!memberRank.getPreferentialScale().toString().matches(regNum) || memberRank.getPreferentialScale()>100){
			renderJson(Ret.fail("info","优惠百分比请输入0-100的正整数!"));
			return;
		}
		if(!memberRank.getPoint().toString().matches(regNum)){
			renderJson(Ret.fail("info","所需积分请填写0或正整数字!"));
			return;
		}
		if(!memberRank.getMoneyNum().toString().matches(regNum)){
			renderJson(Ret.fail("info","所需消费金额请填写0或正整数字!"));
			return;
		}
		if (equalPointMemberRank != null && !equalPointMemberRank.getStr("id").equalsIgnoreCase(persistent.getStr("id"))) {
			renderJson(Ret.fail("info","已存在相同积分的会员等级!"));
			return;
		}
		//至少有一个默认
		if(!memberRank.getBoolean("isDefault") && MemberRank.dao.findById(memberRank.getStr("id")).getBoolean("isDefault")){
			renderJson(Ret.fail("info","至少有一个默认会员等级!"));
			return;
		}
		memberRank.update(memberRank);
		renderJson(Ret.ok("info","修改成功!").set("url","list"));
	}
	
	// 删除
	public void delete() {
		shopId=getAttr("shopId");
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			System.out.println(id);
			MemberRank memberRank = MemberRank.dao.findById(id);
			// 等级下存在会员
			List<Member> member = memberRank.getMember();
			
			// 获取所有实体对象总数.
			long totalCount = memberRank.getTotalCount(shopId);
			
			if(member != null && member.size() > 0){
				renderJson(Ret.fail("info","会员等级[" + memberRank.getStr("name") + "]下存在会员,删除失败!"));
			}else if(ids.length >= totalCount){
				renderJson(Ret.fail("info","删除失败,必须至少保留一个会员等级!"));
			}else{
				MemberRank.dao.deleteById(id);
				renderJson(Ret.ok("info","删除成功!"));
			}
		}
	}

}
